/**
 * File Name: XmlDriveSelenium.java
 * Description: 
 * Create Time: 2010-12-16
 * @Author: yue.chen  ( yue.chen2@hisoft.com )
 */

package com.safx.selenium;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.apache.log4j.Logger;

import com.safx.entity.constants.EvalJS;
import com.safx.entity.constants.Path;
//import com.hisoft.constant.Path;
import com.safx.entity.env.Environment;
import com.safx.entity.env.SheduleEntity;
import com.safx.entity.env.VariableMap;
import com.safx.entity.enums.ResultEnum;
import com.safx.utility.Base64;
import com.safx.utility.CommonTool;
import com.thoughtworks.selenium.Selenium;
import com.thoughtworks.selenium.SeleniumException;

/**
 * 
 * <b> </b>
 * 
 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
 */
public class XmlDriveSelenium {
	protected ScheduledThreadPoolExecutor executor = null;
	// log4j日志记录
	Logger logger = Logger.getLogger(XmlDriveSelenium.class);
	// selenium对象实例化
	public Selenium selenium = SeleniumX.getInstance();
	// environment对象实例化
	public Environment environment = Environment.getInstance();
	// 超时时间
	public String timeout = environment.getConfig().getTimeout();
	// 浏览器窗口名字，每开一次+1，保證每次開出來的瀏覽器窗口名字都不一樣，方便selenium定位。
	protected int g_windowID = 2012;

	/**
	 * <b></b><br>
	 * Opens an URL in the test frame. This accepts both relative and absolute
	 * URLs. The "open" command waits for the page to load before proceeding,
	 * ie. the "AndWait" suffix is implicit. Note: The URL must be on the same
	 * domain as the runner HTML due to security restrictions in the browser
	 * (Same Origin Policy). If you need to open an URL on another domain, use
	 * the Selenium Server to start a new browser session on that domain. and
	 * call waitForPageToLoad.
	 * 
	 * @param url 网页地址
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-26
	 */
	public String open(String url) {
		onlyOpen(url);
		waitForPageToLoad();
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * <b>只打開瀏覽器，沒有wait</b>
	 * 
	 * @return
	 * @param url 网页地址
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-3
	 */
	public String onlyOpen(String url) {
		selenium.open(url);
		return "" + ResultEnum.PASS;
	}

	/**
	 * Sets the value of an input field, as though you typed it in.
	 * 
	 * <p>
	 * Can also be used to set the value of combo boxes, check boxes, etc. In
	 * these cases, value should be the value of the option selected, not the
	 * visible text.
	 * </p>
	 * 
	 * @return
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2010-12-23
	 */
	public String type(String xpath, String value) {
		selenium.type(xpath, value);
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>click And Wait</b> <br>
	 * Clicks on a link, button, checkbox or radio button. If the click action
	 * causes a new page to load (like a link usually does), call
	 * waitForPageToLoad. <br>
	 * 
	 * @return
	 * 
	 * @throws TargetNotFoundException
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2010-12-23
	 */
	public String clickAndWait(String locator) {
		click(locator);
		waitForPageToLoad();
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b></b><br>
	 * 在safari的浏览器下 click事件 不能触发元素的onclick时间，需要用代码来实现。 <br/>
	 * 1. 获得onclick的javascript再执行. <br/>
	 * 2. window.jQuery('#btn_email_edit').click() 有些是这样才能执行页面跳转。<br/>
	 * 3. 但是当控件是复选框时,他能正确触发 onclick时间。浮云。<br/>
	 * 
	 * @return
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String click(String locator) {
		if (environment.getConfig().getBrowser().contains("safari")) {
			// 1
			String script1 = getAttributeV2(locator + "/@onclick");
			String type = getAttributeV2(locator + "/@type");
			if (!CommonTool.isEmpty(script1) && !type.equals("checkbox")) {
				runScriptV2(script1);
			} else {
				// 2
				String script2 = getAttributeV2(locator + "/@id");
				if (!CommonTool.isEmpty(script2)) {
					runScriptV2("window.jQuery('#" + script2 + "').click()");
				}
			}
		} else {
			selenium.click(locator);
		}
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>等待頁面加載完成</b><br>
	 * 
	 * @return
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String waitForPageToLoad() {
		selenium.waitForPageToLoad(timeout);
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>判斷頁面上的元素是否顯示</b><br>
	 * 
	 * @param locator
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public boolean isElementPresent(String locator) {
		return selenium.isElementPresent(locator);
	}

	/**
	 * <b>判斷頁面上的文本是否顯示</b><br>
	 * 
	 * @param pattern
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public boolean isTextPresent(String pattern) {
		return selenium.isTextPresent(pattern);
	}

	/**
	 * <b></b> <br>
	 * Determines if the specified element is visible. An element can be
	 * rendered invisible by setting the CSS "visibility" property to "hidden",
	 * or the "display" property to "none", either for the element itself or one
	 * if its ancestors. This method will fail if the element is not present. <br>
	 * <br>
	 * locator not found, SeleniumException;
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-19
	 */
	public boolean isVisible(String locator) {
		return selenium.isVisible(locator);
	}

	/**
	 * <b></b><br>
	 * Determines if the specified element is visible. An element can be
	 * rendered invisible by setting the CSS "visibility" property to "hidden",
	 * or the "display" property to "none", either for the element itself or one
	 * if its ancestors. This method will fail if the element is not present. <br>
	 * <br>
	 * locator not found, return false;
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-19
	 */
	public boolean isVisibleV2(String locator) {
		boolean flag = false;
		try {
			flag = selenium.isVisible(locator);
		} catch (SeleniumException e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * <b>單選框或者複選框的選擇，如果不能觸發相關的事件，請嘗試click</b><br>
	 * 
	 * @param str
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String check(String str) {
		selenium.check(str);
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>單選框或者複選框的不選擇，如果不能觸發相關的事件，請嘗試click</b><br>
	 * 
	 * @param str
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String uncheck(String str) {
		selenium.uncheck(str);
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * <b>焦點當前瀏覽器</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public String windowFocus() {
		selenium.windowFocus();
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>执行一段javascript，如果有返回值，就返回</b><br>
	 * 
	 * @param script
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String runScript(String script) {
		selenium.runScript(script);
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * <b>执行一段javascript，如果有返回值，就返回，不会抛出SeleniumException，如果存在则返回空值+pass</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public String runScriptV2(String script) {
		try {
			runScript(script);
		} catch (SeleniumException e) {

		}
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>执行一段javascript，并返回去返回值</b>
	 * 
	 * Create on 08/04/2011
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com)
	 */
	public String getEval(String script) {
		return selenium.getEval(script);
	}

	/**
	 * <b>执行一段javascript，并返回去返回值,不会抛出SeleniumException</b>
	 * 
	 * Create on 08/04/2011
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com)
	 */
	public String getEvalV2(String script) {
		String str = "";
		try {
			str = selenium.getEval(script);
		} catch (SeleniumException e) {
		}
		return str;
	}

	/**
	 * <b>模拟点击浏览器上的back按钮</b><br>
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String goBack() {
		selenium.goBack();
		waitForPageToLoad();
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>获得目标的值，目标可以是XPATH，javascript</b><br>
	 * 
	 * @param xpath
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String getValue(String xpath) {
		String returnString = "";
		if (xpath.contains("/@")) {
			returnString = getAttribute(xpath);
		} else if (xpath.contains("window.")) {
			returnString = CommonTool.threeToSixBit(getEval(xpath));
		} else if (isEditableV2(xpath)) {
			try {
				returnString = getValueT(xpath);
			} catch (SeleniumException e) {
				returnString = getTextV2(xpath);
			}
		} else {
			returnString = getTextV2(xpath);
		}
		return returnString;
	}

	/**
	 * 
	 * selenium原生<code>getValue</code>方法 <b></b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-8-30
	 */
	public String getValueT(String xpath) {
		return selenium.getValue(xpath);
	}

	/**
	 * <b></b><br>
	 * Determines whether the specified input element is editable, ie hasn't
	 * been disabled. This method will fail if the specified element isn't an
	 * input element.
	 * 
	 * @param xpath
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public boolean isEditable(String locator) {
		return selenium.isEditable(locator);
	}

	/**
	 * <b></b><br>
	 * Determines whether the specified input element is editable, ie hasn't
	 * been disabled. This method will fail if the specified element isn't an
	 * input element.
	 * 
	 * @param xpath
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public boolean isEditableV2(String locator) {
		boolean flag = false;
		try {
			flag = selenium.isEditable(locator);
		} catch (SeleniumException e) {
			flag = false;
		}
		return flag;
	}

	/**
	 * <b>模拟浏览器的刷新按钮</b><br>
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String refresh() {
		selenium.refresh();
		waitForPageToLoad();
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b> Sleep for N millissecond. </b>
	 * 
	 * @param millissecond
	 * @throws InterruptedException
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-18
	 */
	public String sleep(int millis) {
		try {
			Thread.sleep(millis);
		} catch (InterruptedException e) {
			logger.error(e.getMessage(), e);
			return "" + ResultEnum.FAIL;
		}
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b> Sleep for N second. </b>
	 * 
	 * @param second
	 * @throws InterruptedException
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-18
	 */
	public String sleep(String second) {
		return sleep(CommonTool.String2Int(second) * 1000);
	}

	/**
	 * <b>关闭当前操作的浏览器，不建议使用。</b><br>
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String close() {
		selenium.close();
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b></b> <br>
	 * Gets the absolute URL of the current page.
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-19
	 */
	public String getLocation() {
		return selenium.getLocation();
	}

	/**
	 * <b>获得xpath属性的值</b><br>
	 * 
	 * @param attributeLocator
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-26
	 */
	public String getAttribute(String attributeLocator) {
		return selenium.getAttribute(attributeLocator);
	}

	/**
	 * 
	 * <b> 获得xpath属性的值</b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-7-11
	 */
	public String attribute(String attributeLocator, String attribute) {
		if (CommonTool.isEmpty(attribute)) {
			return getAttribute(attributeLocator);
		} else {
			// if (environment.getBrowser().toLowerCase().contains("ie")) {
			// return getAttribute(attributeLocator + "@" + attribute);
			// } else {
			return getAttributeV2(attributeLocator + "/@" + attribute);
			// }
		}
	}

	/**
	 * 
	 * <b> 获得xpath属性的值</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-7
	 */
	public String getAttributeV2(String attributeLocator) {
		String returnString = "";
		try {
			returnString = selenium.getAttribute(attributeLocator);
		} catch (SeleniumException se) {
			returnString = "";
		}
		return returnString;
	}

	/**
	 * <b></b><br>
	 * Gets the value of an element attribute. The value of the attribute may
	 * differ across browsers (this is the case for the "style" attribute, for
	 * example).
	 * 
	 * @param attributeLocator
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-2-21
	 */
	public int getXpathCount(String attributeLocator) {
		return (Integer) selenium.getXpathCount(attributeLocator);
	}

	/**
	 * <b></b><br>
	 * Gets all option labels in the specified select drop-down.
	 * 
	 * @param selectLocator
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-26
	 */
	public String[] getSelectOptions(String selectLocator) {
		return selenium.getSelectOptions(selectLocator);
	}

	/**
	 * <b>获得xpath文本的值</b><br>
	 * 
	 * @param locator
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-2-9
	 */
	public String getText(String locator) {
		return selenium.getText(locator).replaceAll("\n ", "").replaceAll("\n", "").trim();
	}

	/**
	 * <b>获得xpath文本的值</b><br>
	 * 
	 * @param locator
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-2-9
	 */
	public String getTextV2(String locator) {
		String returnStr = "";
		try {
			returnStr = getText(locator);
		} catch (SeleniumException e) {
			returnStr = "";
		}
		return returnStr;
	}

	/**
	 * <b>获得下拉列表框选中的标签值</b><br>
	 * 
	 * @param str
	 * @return
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-1-28
	 */
	public String getSelectedLabel(String locator) {
		return selenium.getSelectedLabel(locator);
	}

	/**
	 * <b>Select an option from a drop-down using an option locator.</b><br>
	 * 
	 * @param locator,
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-2-22
	 */
	public String select(String selectLocator, String optionLocator) {
		selenium.select(selectLocator, optionLocator);
		return "" + ResultEnum.PASS;
	}
	
	/**
	 * <b>选择frame</b><br>
	 * 
	 * @param locator
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-2-22
	 */
	public String selectFrame(String locator) {
		selenium.selectFrame(locator);
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b>获得操作系统</b>
	 * 
	 * Create on 08/04/2011
	 * 
	 * @Author yue.chen (yue.chen2@hisoft.com)
	 */
	public String getOS() {
		return getEval(EvalJS.EVALGETOSJS);
	}

	/**
	 * 
	 * @param locator
	 * @param eventName
	 */
	public String fireEvent(String locator, String eventName) {
		selenium.fireEvent(locator, eventName);
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * <b>使xpath获得焦点</b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-7-12
	 */
	public String focus(String locator) {
		selenium.focus(locator);
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * <b> </b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-7-12
	 */
	protected void iAmShedule(final SheduleEntity shedule) {
		executor = new ScheduledThreadPoolExecutor(1);
		executor.schedule(new Runnable() {
			@Override
			public void run() {
				shedule.setTimeout(true);
			}
		}, shedule.getTempTime(), TimeUnit.SECONDS);
	}

	/**
	 * 
	 * <b>等待一个元素的出现</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-9
	 */
	public boolean waitForElementDisplay(String xpath, String time) {
		SheduleEntity entity = new SheduleEntity(CommonTool.String2Int(time));
		iAmShedule(entity);
		int tempTime = 2000;
		while (!entity.isTimeout()) {
			if (isVisibleV2(xpath)) {
				shutdownAndAwaitTermination(executor);
				return true;
			}
			sleep(tempTime);
		}
		throw new SeleniumException("ERROR: wait for '" + xpath + "' " + time + "s time out!");
	}

	/**
	 * 
	 * <b> </b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-30
	 */
	protected void shutdownAndAwaitTermination(ExecutorService pool) {
		pool.shutdown(); // Disable new tasks from being submitted
		try {
			// Wait a while for existing tasks to terminate
			if (!pool.awaitTermination(5, TimeUnit.SECONDS)) {
				pool.shutdownNow(); // Cancel currently executing tasks
				// Wait a while for tasks to respond to being cancelled
				if (!pool.awaitTermination(5, TimeUnit.SECONDS))
					System.err.println("Pool did not terminate");
			}
		} catch (InterruptedException ie) {
			// (Re-)Cancel if current thread also interrupted
			pool.shutdownNow();
			// Preserve interrupt status
			Thread.currentThread().interrupt();
		}
	}

	/**
	 * 
	 * <b>获得target的值</b><br/>
	 * return target
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-30
	 */
	public String getTarget(String target) {
		return target;
	}

	/**
	 * 
	 * <b> </b> Opens a popup window (if a window with that ID isn't already
	 * open). After opening the window, it'll auto to using the selectWindow
	 * command.
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-20
	 */
	public boolean openWindow(String str) {
		/*
		 * open by onclick
		 */
		if (isElementPresent(str)) {
			String onClick = getAttributeV2(str + "/@onclick");
			if (!CommonTool.isEmpty(onClick)) {
				String url = onClick.split("'")[1];
				openNewWindow(url);
				return true;
			}

			/*
			 * open by href
			 */
			String href = getAttributeV2(str + "/@href");
			if (!CommonTool.isEmpty(href)) {
				openNewWindow(href);
				return true;
			}
		} else {
			openNewWindow(str);
			return true;
		}
		return false;

	}

	/**
	 * 
	 * <b>openNewWindow</b><br/>
	 * Opens a popup window (if a window with that ID isn't already open). After
	 * opening the window, it'll auto to using the selectWindow command.
	 * 
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-30
	 */
	protected void openNewWindow(String url) {
		String windowID = getWindowID();
		selenium.openWindow(url, windowID);
		selenium.waitForPopUp(windowID, timeout);
		selenium.selectWindow(windowID);
	}

	/**
	 * 
	 * <b>获得window的ID</b>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-30
	 */
	protected String getWindowID() {
		g_windowID++;
		return "" + g_windowID;
	}

	/**
	 * 
	 * <b> </b> Selects a popup window using number; once a popup window has
	 * been selected, all commands go to that window. To select the main window
	 * again, use "1" as the target.
	 * 
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-28
	 */
	public String selectWindow(String str) {
		int i = CommonTool.String2Int(str) - 1;
		if (i == 0) {
			selenium.selectWindow("null");
			windowFocus();
		} else if (i < getWindowCount() && i > 0) {
			selenium.selectWindow(selenium.getAllWindowNames()[i]);
			windowFocus();
		} else {
			logger.warn("select default Window.");
			selenium.selectWindow("null");
			windowFocus();
		}
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * <b> </b> Returns number of all windows that the browser knows about.
	 * 
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-28
	 */
	protected int getWindowCount() {
		return selenium.getAllWindowNames().length;
	}

	/**
	 * 
	 * <b> </b> Tells the Selenium server to add the specificed key and value as
	 * a custom outgoing request header. This only works if the browser is
	 * configured to use the built in Selenium proxy.
	 * 
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-28
	 */
	public String addCustomRequestHeader(String userName, String password) {
		String authHeader = Base64.encodeBytes((userName + ":" + password).getBytes());
		selenium.addCustomRequestHeader("Authorization", "Basic " + authHeader);
		return "" + ResultEnum.PASS;
	}

	/**
	 * <b></b><br>
	 * Capture a PNG screenshot. It then returns the file as a base 64 encoded
	 * string.
	 * 
	 * @return
	 * @throws IOException
	 * @Author yue.chen (yue.chen2@hisoft.com) Create on 2011-3-29
	 */
	public String captureScreenshotPath() {
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd_HHmmss");
		String pngPath = Path.CAPTURESCREENSHOTPATH + "/" + df.format(new Date()) + ".png";
		try {
			if (!(new File(Path.REPORTPATH).isDirectory())) {
				new File(Path.REPORTPATH).mkdir();
			}
			if (!(new File(Path.CAPTURESCREENSHOTPATH).isDirectory())) {
				new File(Path.CAPTURESCREENSHOTPATH).mkdir();
			}
		} catch (SecurityException e) {
			logger.error(e.getMessage(), e);
		}
		try {
			String pngBase64 = captureScreenshot();
			Base64.decodeToFile(pngBase64, pngPath);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
		sleep(1000);
		return pngPath.replace(Path.REPORTPATH + "/", "");
	}

	/**
	 * 截图功能，返回值是图片的base64码 如果浏览器是firefox调用captureEntirePageScreenshotToString方法，
	 * 其他则调用captureScreenshotToString
	 * 
	 * @return
	 */
	public String captureScreenshot() {
		selenium.windowFocus();
		selenium.windowMaximize();
		if (environment.getConfig().getBrowser().toLowerCase().contains("firefox") && environment.getConfig().getHost().equals("127.0.0.1")) {
			return selenium.captureEntirePageScreenshotToString("");
		}
		return selenium.captureScreenshotToString();
	}

	/**
	 * 截图功能，参数是存放路径
	 * 
	 * @param pngPath
	 * @return
	 */
	public String saveCaptureScreenshot(String pngPath) {
		try {
			Base64.decodeToFile(captureScreenshot(), pngPath);
		} catch (IOException e) {
			logger.error(e.getMessage(), e);
		}
		return pngPath;
	}

	/**
	 * 
	 * <b>往map中塞值 key-value形式 key存在就更新 </b><br/>
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-6-30
	 */
	public String setProperty(String key, String value) {
		VariableMap.getInstance().mapPutKV(key, value);
		return "" + ResultEnum.PASS;
	}

	/**
	 * 
	 * Simulates a user pressing the left mouse button (without releasing it
	 * yet) on the specified element.
	 * 
	 * Parameters: locator an element locator
	 * 
	 * @Author chenyue ( yue.chen2@hisoft.com ) Create on 2011-7-11
	 */
	public String mouseDown(String str) {
		selenium.mouseDown(str);
		return "" + ResultEnum.PASS;
	}

	/**
	 * Simulates the event that occurs when the user releases the mouse button
	 * (i.e., stops holding the button down) on the specified element.
	 * 
	 * Parameters: locator an element locator
	 * 
	 * @param str
	 * @return
	 */
	public String mouseUp(String str) {
		selenium.mouseUp(str);
		return "" + ResultEnum.PASS;
	}

	/**
	 * 截图功能，参数是存放路径
	 * 
	 * @param screenShotname
	 * @return
	 */
	public String screenshot(String screenShotname) {
		// 目录不存在就创建目录
		try {
			if (!(new File(Path.REPORTPATH).isDirectory())) {
				new File(Path.REPORTPATH).mkdir();
			}
			if (!(new File(Path.LOCALSCREENSHOTPATH + environment.getConfig().getLanguage()).isDirectory())) {
				new File(Path.LOCALSCREENSHOTPATH + environment.getConfig().getLanguage()).mkdir();
			}
		} catch (SecurityException e) {
			logger.error(e.getMessage());
		}
		// 保存截图
		saveCaptureScreenshot(Path.LOCALSCREENSHOTPATH + environment.getConfig().getLanguage() + "/" + screenShotname + ".png");
		return "" + ResultEnum.PASS;
	}
}
